package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.ysd.entity.Company;
import com.ysd.util.DBManager;

public class CompanyDaoImpl {
	public Company getCompany() {
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		Company company = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery("SELECT * FROM company");
			while(rs.next()) {
				String name = rs.getString("name");
				String website = rs.getString("website");
				String introduction = rs.getString("introduction");
				String phone = rs.getString("phone");
				String email = rs.getString("email");
				String address = rs.getString("address");
				String culture = rs.getString("culture");
				String logo = rs.getString("logo");
				String assembly=rs.getString("assembly");
				String customized = rs.getString("customized");
				 int model = rs.getInt("model");
				 String byColor = rs.getString("byColor");
				 String designElements = rs.getString("designElements");
				 String style =rs.getString("style");
				 String stylePositioning = rs.getString("stylePositioning");
				
				 company = new Company(name, website, introduction, phone, email, address, culture, logo, assembly, customized, model, byColor, designElements, style, stylePositioning);
						 
			}
			return company;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	public Company selCompany() {
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		Company company = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery("SELECT * FROM company");
			
			while(rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				String website = rs.getString("website");
				String introduction = rs.getString("introduction");
				String phone = rs.getString("phone");
				String email = rs.getString("email");
				String address = rs.getString("address");
				String culture = rs.getString("culture");
				String logo = rs.getString("logo");
				 company = new Company(id, name, website, introduction, phone, email, address, culture, logo);
						 
			}
			System.out.println(company);
			return company;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	/**
	 * 通过id修改
	 * @param id
	 * @return
	 */
	public Company getCompanyById(int id) {
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		Company c = null;
		try {
			st=conn.createStatement();
			rs = st.executeQuery("select * from company where id="+id);
			while(rs.next()) {
				int id1 = rs.getInt("id");
				String name = rs.getString("name");
				String website = rs.getString("website");
				String introduction = rs.getString("introduction");
				String phone = rs.getString("phone");
				String email = rs.getString("email");
				String address = rs.getString("address");
				String culture = rs.getString("culture");
				String logo = rs.getString("logo");
				 c = new Company(id1, name, website, introduction, phone, email, address, culture, logo);
				
			}
			return c;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public int updateCompany(Company c) {
		String sql="update company set name='"+c.getName()+"'," + 
				"website='"+c.getWebsite()+"'," + 
				"introduction='"+c.getIntroduction()+"'," + 
				"phone='"+c.getPhone()+"'," + 
				"email='"+c.getEmail()+"', "+ 
				"address='"+c.getAddress()+"'," + 
				"culture='"+c.getCulture()+"'," + 
				"logo='"+c.getLogo()+"'" + 
				" where id="+c.getId();
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
}
